package edu.moravian.UI;

import edu.moravian.entity.Tower;
import edu.moravian.entity.TowerProperties;
import edu.moravian.grid.Grid;
import edu.moravian.grid.GridCell;
import edu.moravian.math.Point2D;
import edu.moravian.math.Vector2D;
import edu.moravian.player.PlayerInfo;
import edu.moravian.world.World2D;
import edu.moravian.world.WorldDescriptor;
import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 *
 * @author ronaghan
 */
public class TowerSelectionView extends GameView
{

    private Grid selectGrid;
    private double width;
    private double height;
    private PlayAreaView playerAreaView;
    private TowerUpgradeView towerUpgradeView;

    public TowerSelectionView(WorldDescriptor boundsDescriptor, PlayAreaView playerAreaView, TowerUpgradeView towerUpgradeView)
    {
        super(boundsDescriptor);
        this.playerAreaView = playerAreaView;
        this.towerUpgradeView = towerUpgradeView;
        selectGrid = setupGrid();



        this.width = boundsDescriptor.getWidth();
        this.height = boundsDescriptor.getHeight();
    }

    private Grid setupGrid()
    {

        List<Tower> towers = new ArrayList<Tower>();


        File towersDir = new File("src/main/resources/towers");

        for (File file : towersDir.listFiles())
        {
            if (file.isFile() && file.canRead())
            {
                Properties towerProp = new Properties();
                try
                {
                    towerProp.load(new FileInputStream(file));

                    String type = towerProp.getProperty("type");
                    double radius = Double.parseDouble(towerProp.getProperty("radius"));
                    double cooldownTime = Double.parseDouble(towerProp.getProperty("cooldownTime"));
                    double fireRadius = Double.parseDouble(towerProp.getProperty("fireRadius"));

                    int initialCost = Integer.parseInt(towerProp.getProperty("initialCost"));
                    int sellBack = Integer.parseInt(towerProp.getProperty("sellBackCost"));
                    String description = towerProp.getProperty("description");
                    
                    TowerProperties props = new TowerProperties(initialCost, sellBack, description);

                    Tower tower = new Tower(new Point2D(), new Vector2D(), radius, cooldownTime, fireRadius, type, props);
                    towers.add(tower);

                }
                catch (IOException ex)
                {
                    ex.printStackTrace();
                    System.exit(-1);
                }
            }
        }

        int gridDim = (int) Math.ceil(Math.sqrt((double) towers.size()));

        Grid grid = new Grid(gridDim, gridDim, boundsDescriptor);

        int r = gridDim - 1;
        int c = 0;
        int curEl = 0;
        for (int i = 0; i < towers.size(); i++)
        {
            curEl = r * gridDim + c;

            grid.getCell(curEl).setEntity(towers.get(i));

            c++;
            if (c == gridDim)
            {
                r -= 1;
                c = 0;
            }
        }


        return grid;
    }

    public synchronized void draw(World2D w2d)
    {
        w2d.setColor(Color.WHITE);
        w2d.setOpacity(0.5f);
        w2d.drawCenteredRectangle(boundsDescriptor.getCenter(), width * .99, height * .99);
        w2d.setOpacity(1.0f);


        selectGrid.draw(w2d);


        for (GridCell cell : selectGrid.getGrid())
        {
            if (cell.hasEntity())
            {
                if (cell.getEntity() instanceof Tower)
                {
                    Tower t = (Tower) cell.getEntity();

                    if (PlayerInfo.getMonies() < t.getTowerProps().getCost())
                    {
                        w2d.setOpacity(.5f);
                        w2d.drawCenteredImage(ImageManager.getTower(t.getType()), cell.getCenter(), selectGrid.getCellWidth() * .8, selectGrid.getCellHeight() * .8);
                        w2d.setOpacity(1.0f);
                    }
                    else
                    {
                        w2d.drawCenteredImage(ImageManager.getTower(t.getType()), cell.getCenter(), selectGrid.getCellWidth() * .8, selectGrid.getCellHeight() * .8);
                    }
                }
            }
        }
    }

    public synchronized void handleMouseClick(Point2D location)
    {
        GridCell cellSelected = selectGrid.getCell(location.getX(), location.getY());
        Tower tower = (Tower) (cellSelected.getEntity());
        Tower toSet = new Tower(tower);
        if (PlayerInfo.getMonies() >= toSet.getTowerProps().getCost())
        {
            playerAreaView.setCurrTower(toSet);
        }
    }

    public synchronized void handleMouseMoved(Point2D location)
    {
        GridCell cellSelected = selectGrid.getCell(location.getX(), location.getY());
        Tower tower = (Tower) (cellSelected.getEntity());
        Tower toSet = new Tower(tower);
        towerUpgradeView.setCurPlaceTower(toSet);
    }

}
